package com.yunxi.service.localService.vehicle.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.vehicle.VehicleSpectrumMapper;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.vehicle.VehicleSpectrum;
import com.yunxi.service.localService.dealer.PartnerService;
import com.yunxi.service.localService.vehicle.VehicleSpectrumService;
import com.yunxi.vehicle.pojo.VehicleSpectrumReqVO;
import com.yunxi.vehicle.pojo.VehicleSpectrumRespVO;
import com.yunxi.vehicle.pojo.VehicleSpectrumVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产车型型谱 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-16
 */
@Service
public class VehicleSpectrumServiceImpl extends ServiceImplPlus<VehicleSpectrumMapper, VehicleSpectrum> implements VehicleSpectrumService {

    @Autowired
    @Qualifier(value="partnerServiceImpl")
    PartnerService partnerService;

    @Override
    public Object findTreeList() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        List list = baseMapper.queryByList(map);
        TreeConfig config = new TreeConfig("-1", "型谱", true, "nodeCode", "nodeName", "parentNodeCode", "hasChild");
        return config.toTreeData(list,false);
    }

    @Override
    public Object treeAlias() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        List list = baseMapper.queryByList(map);
        TreeConfig config = new TreeConfig("-1", "型谱", true, "nodeCode", "aliasName", "parentNodeCode", "hasChild");
        return config.toTreeData(list,false);
    }

    @Override
    public VehicleSpectrumVO loadByNodeCode(String nodeCode) {
        return baseMapper.loadByNodeCode(nodeCode);
    }

    @Override
    public Object treeFull() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        List list = baseMapper.queryByList(map);
        TreeConfig config = new TreeConfig("-1", "型谱", true, "nodeCode", "fullName", "parentNodeCode", "hasChild");
        return config.toTreeData(list,false);
    }

    @Override
    public Object findTreeList(LoginInfo loginInfo) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();

        PartnerVO partnerVO = partnerService.getByPartnerCode(loginInfo.getPartnerCode());
        if(partnerVO!=null){
            if(StringUtils.isNotEmpty(partnerVO.getProductAuthCodeJson())){
                String node_in = partnerVO.getProductAuthCodeJson().replaceAll("\\[","").replaceAll("\\]","");
                map.put("nodeCodeIn",node_in);
            }
        }
        List list = baseMapper.queryByList(map);
        TreeConfig config = new TreeConfig("-1", "型谱", true, "nodeCode", "nodeName", "parentNodeCode", "hasChild");
        return config.toTreeData(list,false);
    }

    @Override
    public boolean delAll() {
        return baseMapper.delete(null) > 0;
    }

    @Override
    public PageInfo<VehicleSpectrumRespVO> queryVehicleSpectrumByApp(VehicleSpectrumReqVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<VehicleSpectrumRespVO> result = baseMapper.queryVehicleSpectrumByApp(vo);
        return new PageInfo<>(result);
    }

    @Override
    public List<VehicleSpectrumRespVO> queryVehicleSpectrumByApp(VehicleSpectrumReqVO vo){
           return baseMapper.queryVehicleSpectrumByApp(vo);
    }

    @Override
    public List<VehicleSpectrumRespVO> queryVehicleSpectrumByAppLast(VehicleSpectrumReqVO vo){
        return baseMapper.queryVehicleSpectrumByAppLast(vo);
    }
}
